package com.itic.appbase.framework.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 命令行工具类
 * <p>Company: itic</p>
 * 
 * @author: bianyuwu
 * @date: 2015年5月8日 上午8:45:57
 * @version: V1.0
 */
public class CommandHelper {

    private static final Logger log = LoggerFactory.getLogger(CommandHelper.class);
    private CommandHelper() {
    }
    
    /**
     * 执行命令语句.
     * 
     * @param command 要执行的命令
     * @return exitVal 执行语句的返回值0代表正常结束
     */
    public static int runCommand(String command) {
        int exitVal = -1;
        StreamGobbler input = null;
        StreamGobbler error = null;
        Process pro = null;
        try {
            pro = Runtime.getRuntime().exec(command);
            error = new StreamGobbler(pro.getErrorStream(), "Error");
            input = new StreamGobbler(pro.getInputStream(), "Input");
            error.start();
            input.start();
            exitVal = pro.waitFor();
            log.debug(command + " : ExitValue is " + exitVal);
        } catch (Exception e) {
            log.error(command + " 执行出错。",e);
        } finally {
            if (pro != null) {
                pro.destroy();
                pro = null;
            }
        }
        return exitVal;
    }

}

/**
 * 用于处理Runtime.getRuntime().exec产生的错误流及输出流
 */
class StreamGobbler
        extends Thread {
    private static final Logger log = LoggerFactory.getLogger(StreamGobbler.class);
    InputStream is;
    String type;
    OutputStream os;
    
    StreamGobbler(InputStream is, String type) {
        this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect) {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }
    
    @Override
    public void run() {
        InputStreamReader isr = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        log.info(type + " : start");
        try {
            if (os != null) {
                pw = new PrintWriter(os);
            }
            if (System.getProperty("os.name").contains("Windows")) {
                isr = new InputStreamReader(is, "gbk");
            } else {
                isr = new InputStreamReader(is, "utf-8");
            }
            br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (pw != null) {
                    pw.println(line);
                }
                log.debug(type + ">" + line);
            }
            log.debug(type + " complete!");
            if (pw != null) {
                pw.flush();
            }
        } catch (IOException ioe) {
            log.error("读取流出错。", ioe);
        } finally {
            try {
                if (isr != null) {
                    isr.close();
                }
                if (br != null) {
                    br.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                log.error("关闭流出错。", e);
            }
        }
    }
}
